<?php
$GLOBALS['colour_schemes'] = array(
	1 => 'Facebook Blue|3B5998,F7F7F7,000,555,D8DFEA,EEE,FFA,DD9,3B5998,FFF,FFF',
	2 => 'Digu Orange|b50,ddd,111,555,fff,eee,ffa,dd9,e81,c40,fff',
	3 => 'Fanfou Blue|13819F,E7F2F5,333,555,fff,E7F2F5,FFA,DD9,00CCFF,333,333',
	4 => 'Colorful|535F74,D1D0B4,000,555,FFEDED,FFD3D3,FFA,DD9,D33D3E,FFF,FFF',
	5 => 'Twitter Blue|1481B1,FFF,333,555,FFF,EEE,FFA,DD9,9AE4E8,333,333',
	6 => 'Whimsical Pink|c06,fcd,623,c8a,fee,fde,ffa,dd9,C06,fee,fee',
	7 => 'Green|293C03,ccc,000,555,fff,eee,CCE691,ACC671,495C23,919C35,fff',
	8 => 'Purple|BAAECB,1F1530,9C8BB5,6D617E,362D45,4C4459,4A423E,5E5750,191432,6D617E,6D617E',
);

menu_register(array(
	'settings' => array(
		'callback' => 'settings_page',
		'title' => __("Settings"),
	),
	'reset' => array(
		'hidden' => true,
		'callback' => 'cookie_monster',
	),
));

function cookie_monster() {
	$cookies = array(
		'browser',
		'settings',
		'utc_offset',
		'search_favourite',
		'USER_AUTH',
	);
	$duration = time() - 3600;
	foreach ($cookies as $cookie) {
		setcookie($cookie, NULL, $duration, '/');
		setcookie($cookie, NULL, $duration, WEB_ROOT);
		setcookie($cookie, NULL, $duration);
	}
	return theme('page', __("Cookie Monster"), "<p>".__("The cookie monster has logged you out and cleared all settings. Try <a href='".BASE_URL."'>logging in again</a> now.")."</p>");
}

function setting_fetch($setting, $default = NULL) {
	$settings = (array) unserialize(base64_decode($_COOKIE['settings']));
	if (array_key_exists($setting, $settings)) {
		return $settings[$setting];
	} else {
		return $default;
	}
}

function settings_page($args) {
	if ($args[1] == 'save') {
		$settings['browser'] = $_POST['browser'];
		$settings['tpp'] = $_POST['tpp'];
		$settings['gwt'] = $_POST['gwt'];
		$settings['ort'] = $_POST['ort'];
		$settings['locale'] = $_POST['locale'];
		$settings['colours'] = $_POST['colours'];
		$settings['reverse'] = $_POST['reverse'];

		$settings['topuser'] = $_POST['topuser'];
		$settings['tophome'] = $_POST['tophome'];
		$settings['topreplies'] = $_POST['topreplies'];
		$settings['topretweets'] = $_POST['topretweets'];
		$settings['topdirects'] = $_POST['topdirects'];
		$settings['toptwitpic'] = $_POST['toptwitpic'];
		$settings['topsearch'] = $_POST['topsearch'];
		
		$settings['replies'] = $_POST['replies'];
		$settings['retweets'] = $_POST['retweets'];
		$settings['directs'] = $_POST['directs'];
		$settings['search'] = $_POST['search'];
		$settings['public'] = $_POST['public'];
		$settings['favourites'] = $_POST['favourites'];
		$settings['lists'] = $_POST['lists'];
		$settings['followers'] = $_POST['followers'];
		$settings['friends'] = $_POST['friends'];
		$settings['blockings'] = $_POST['blockings'];
		$settings['trends'] = $_POST['trends'];
		$settings['twitpic'] = $_POST['twitpic'];
		$settings['about'] = $_POST['about'];
		$settings['ssettings'] = $_POST['ssettings'];
		$settings['slogout'] = $_POST['slogout'];
		$settings['srefresh'] = $_POST['srefresh'];

		$settings['linktrans'] = $_POST['linktrans'];
		$settings['avataro'] = $_POST['avataro'];
		$settings['buttonintext'] = $_POST['buttonintext'];
		$settings['moreinreply'] = $_POST['moreinreply'];
		
		$settings['buttonre'] = $_POST['buttonre'];
		$settings['buttondm'] = $_POST['buttondm'];
		$settings['buttonfav'] = $_POST['buttonfav'];
		$settings['buttonrt'] = $_POST['buttonrt'];
		$settings['buttondel'] = $_POST['buttondel'];
		$settings['buttontime'] = $_POST['buttontime'];
		$settings['buttonfrom'] = $_POST['buttonfrom'];
		$settings['buttonend'] = $_POST['buttonend'];
		
		$settings['short'] = $_POST['short'];
		$settings['longurl'] = $_POST['longurl'];
		$settings['showthumbs'] = $_POST['showthumbs'];
		$settings['fixedtagspre'] = $_POST['fixedtagspre'];
		$settings['fixedtagspost'] = $_POST['fixedtagspost'];
		$settings['rtsyntax'] = $_POST['rtsyntax'];
		$settings['css'] = $_POST['css'];
		$duration = time() + (3600 * 24 * 365);
		setcookie('settings', base64_encode(serialize($settings)), $duration, '/');
		settings_refresh('');
	}
	
	$modes = array(
		'mobile' => __("Normal phone"),
		'touch' => __("Touch phone"),
		'desktop' => __("PC/Laptop"),
		//'text' => __("Text only (For UCWeb Better)"),
		'blackberry' => __("BlackBerry (Page At Bottom)"),
		//'worksafe' => __("Work Safe"),
		'naiping' => __("Nai Ping (For PC Only)"),
	);
	
	$gwt = array(
		'off' => __("direct"),
		'on' => __("Via GWT"),
	);

	$ort = array(
		'no' => 'No',
		'yes' => 'Yes',
	);

	$tpp = array(
		20 => '20',
		40 => '40',
		60 => '60',
		80 => '80',
		120 => '120',
		200 => '200',
	);

	$short = array(
		'no' => __("Not using"),
		'is.gd' => 'is.gd',
		'tr.im' => 'tr.im',
		'goo.gl' => 'goo.gl',
		'tinyurl.com' => 'tinyurl.com',
		'j.mp' => 'j.mp',
		'r.im' => 'r.im',
	);

	$locale = array(
		'zh_CN' => 简体中文,
		'en_US' => English,
		'zh_TW' => 繁體中文,
	);

	$colour_schemes = array();
	foreach ($GLOBALS['colour_schemes'] as $id => $info) {
		list($name, $colours) = explode('|', $info);
		$colour_schemes[$id] = $name;
	}

	$content .= '<form action="'.BASE_URL.'settings/save" method="post">';
	$content .= '<p>'.__("Configure Menu Items").'<br />';
	$content .= '<small>'.__("Choose what you want to display on the Top Bar.").'</small><br />';
	$content .= '<label><input type="checkbox" name="topuser" value="yes" '. (setting_fetch('topuser') == 'yes' ? ' checked="checked" ' : '') .' /> '.__("User").'</label><br />';
	$content .= '<label><input type="checkbox" name="tophome" value="yes" '. (setting_fetch('tophome', 'yes') == 'yes' ? ' checked="checked" ' : '') .' /> '.__("Home").'</label><br />';
	$content .= '<label><input type="checkbox" name="topreplies" value="yes" '. (setting_fetch('topreplies', 'yes') == 'yes' ? ' checked="checked" ' : '') .' /> '.__("Replies").'</label><br />';
	$content .= '<label><input type="checkbox" name="topretweets" value="yes" '. (setting_fetch('topretweets', 'yes') == 'yes' ? ' checked="checked" ' : '') .' /> '.__("Retweets").'</label><br />';
	$content .= '<label><input type="checkbox" name="topdirects" value="yes" '. (setting_fetch('topdirects', 'yes') == 'yes' ? ' checked="checked" ' : '') .' /> '.__("Directs").'</label><br />';
	$content .= '<label><input type="checkbox" name="topsearch" value="yes" '. (setting_fetch('topsearch') == 'yes' ? ' checked="checked" ' : '') .' /> '.__("Search").'</label><br />';
	$content .= '<label><input type="checkbox" name="toptwitpic" value="yes" '. (setting_fetch('toptwitpic') == 'yes' ? ' checked="checked" ' : '') .' /> '.__("Twitpic").'</label><br />';
	$content .= '<small>'.__("And Choose what you want to display on the Bottom Bar.").'</small><br />';
	$content .= '<label><input type="checkbox" name="replies" value="yes" '. (setting_fetch('replies') == 'yes' ? ' checked="checked" ' : '') .' /> '.__("Replies").'</label><br />';
	$content .= '<label><input type="checkbox" name="retweets" value="yes" '. (setting_fetch('retweets') == 'yes' ? ' checked="checked" ' : '') .' /> '.__("Retweets").'</label><br />';
	$content .= '<label><input type="checkbox" name="directs" value="yes" '. (setting_fetch('directs') == 'yes' ? ' checked="checked" ' : '') .' /> '.__("Directs").'</label><br />';
	$content .= '<label><input type="checkbox" name="search" value="yes" '. (setting_fetch('search') == 'yes' ? ' checked="checked" ' : '') .' /> '.__("Search").'</label><br />';
	$content .= '<label><input type="checkbox" name="twitpic" value="yes" '. (setting_fetch('twitpic') == 'yes' ? ' checked="checked" ' : '') .' /> '.__("Twitpic").'</label><br />';
	$content .= '<label><input type="checkbox" name="favourites" value="yes" '. (setting_fetch('favourites') == 'yes' ? ' checked="checked" ' : '') .' /> '.__("Favourites").'</label><br />';
	$content .= '<label><input type="checkbox" name="lists" value="yes" '. (setting_fetch('lists') == 'yes' ? ' checked="checked" ' : '') .' /> '.__("Lists").'</label><br />';
	$content .= '<label><input type="checkbox" name="followers" value="yes" '. (setting_fetch('followers') == 'yes' ? ' checked="checked" ' : '') .' /> '.__("Followers").'</label><br />';
	$content .= '<label><input type="checkbox" name="friends" value="yes" '. (setting_fetch('friends') == 'yes' ? ' checked="checked" ' : '') .' /> '.__("Friends").'</label><br />';
	$content .= '<label><input type="checkbox" name="blockings" value="yes" '. (setting_fetch('blockings') == 'yes' ? ' checked="checked" ' : '') .' /> '.__("Blockings").'</label><br />';
	$content .= '<label><input type="checkbox" name="public" value="yes" '. (setting_fetch('public') == 'yes' ? ' checked="checked" ' : '') .' /> '.__("Public").'</label><br />';
	$content .= '<label><input type="checkbox" name="trends" value="yes" '. (setting_fetch('trends') == 'yes' ? ' checked="checked" ' : '') .' /> '.__("Trends").'</label><br />';
	$content .= '<label><input type="checkbox" name="about" value="yes" '. (setting_fetch('about') == 'yes' ? ' checked="checked" ' : '') .' /> '.__("About").'</label><br />';
	$content .= '<label><input type="checkbox" name="ssettings" value="yes" '. (setting_fetch('ssettings', 'yes') == 'yes' ? ' checked="checked" ' : '') .' /> '.__("Settings").'</label><br />';
	$content .= '<label><input type="checkbox" name="slogout" value="yes" '. (setting_fetch('slogout', 'yes') == 'yes' ? ' checked="checked" ' : '') .' /> '.__("Logout").'</label><br />';
	$content .= '<label><input type="checkbox" name="srefresh" value="yes" '. (setting_fetch('srefresh') == 'yes' ? ' checked="checked" ' : '') .' /> '.__("Refresh").'</label></p><hr />';
	$content .= '<p>'.__("Colour scheme").':<br /><select name="colours">'.theme('options', $colour_schemes, setting_fetch('colours', 1)).'</select></p><hr />';
	$content .= '<p>'.__("Mode").':<br /><select name="browser">'.theme('options', $modes, $GLOBALS['current_theme']).'</select></p><hr />';
	$content .= '<p><label><input type="checkbox" name="buttonintext" value="yes" '. (setting_fetch('buttonintext', 'yes') == 'yes' ? ' checked="checked" ' : '') .' /> '.__("Show @/DM/RT/FAV/DEL's button As Text").'</label><br />';
	$content .= '<small>'.__("And Choose What you Want to Display On each Status.").'</small><br />';
	$content .= '<label><input type="checkbox" name="buttonre" value="yes" '. (setting_fetch('buttonre', 'yes') == 'yes' ? ' checked="checked" ' : '') .' /> '.__("@").'</label>';
	$content .= '<label><input type="checkbox" name="buttondm" value="yes" '. (setting_fetch('buttondm') == 'yes' ? ' checked="checked" ' : '') .' /> '.__("DM").'</label>';
	$content .= '<label><input type="checkbox" name="buttonfav" value="yes" '. (setting_fetch('buttonfav', 'yes') == 'yes' ? ' checked="checked" ' : '') .' /> '.__("FAV").'</label>';
	$content .= '<label><input type="checkbox" name="buttonrt" value="yes" '. (setting_fetch('buttonrt', 'yes') == 'yes' ? ' checked="checked" ' : '') .' /> '.__("RT").'</label>';
	$content .= '<label><input type="checkbox" name="buttondel" value="yes" '. (setting_fetch('buttondel', 'yes') == 'yes' ? ' checked="checked" ' : '') .' /> '.__("DEL").'</label><br />';
	$content .= '<label><input type="checkbox" name="buttontime" value="yes" '. (setting_fetch('buttontime', 'yes') == 'yes' ? ' checked="checked" ' : '') .' /> '.__("Status Times").'</label>';
	$content .= '<label><input type="checkbox" name="buttonfrom" value="yes" '. (setting_fetch('buttonfrom', 'yes') == 'yes' ? ' checked="checked" ' : '') .' /> '.__("Status From").'</label></p><hr>';
	$content .= '<p><label><input type="checkbox" name="buttonend" value="yes" '. (setting_fetch('buttonend') == 'yes' ? ' checked="checked" ' : '') .' /> '.__("Put the Buttons after each Status").'</label></p><hr>';
	$content .= '<p><label><input type="checkbox" name="moreinreply" value="yes" '. (setting_fetch('moreinreply') == 'yes' ? ' checked="checked" ' : '') .' /> '.__("Show Status in Reply Page").'</label></p><hr>';
	$content .= '<p><label><input type="checkbox" name="avataro" value="yes" '. (setting_fetch('avataro', 'yes') == 'yes' ? ' checked="checked" ' : '') .' /> '.__("Disable Avatar").'</label></p><hr>';
	$content .= '<p>'.__("Tweets per page").' (20-200): <input type="text" id="tpp" name="tpp" value="'.setting_fetch('tpp', 20).'" maxlength="3" style="width:20px;"/></p><hr />';
	
	$content .= '<p>'.__("Display Official Retweets in your reply timeline").':<br /><select name="ort">'.theme('options', $ort, setting_fetch('ort', 'no')).'</select></p><hr />';
	
	$content .= '<p>'.__("External links go").':<br /><select name="gwt">'.theme('options', $gwt, setting_fetch('gwt', $GLOBALS['current_theme'] == 'text' ? 'on' : 'off')).'</select><br /><small>'.__("Google Web Transcoder (GWT) converts third-party sites into small, speedy pages suitable for older phones and people with less bandwidth.").'</small></p><hr />';
	$content .= '<p><label><input type="checkbox" name="linktrans" value="yes" '. (setting_fetch('linktrans') == 'yes' ? ' checked="checked" ' : '') .' /> '.__("Change URL to [link]").'</label></p><hr />';
	$content .= '<p>'.__("Short URL Services").':<br /><select name="short">'.theme('options', $short, setting_fetch('short', 'is.gd')).'</select></p><hr />';
	if (LONG_URL == 1) {$content .= '<p><label><input type="checkbox" name="longurl" value="yes" '. (setting_fetch('longurl') == 'yes' ? ' checked="checked" ' : '') .' /> '.__("Show Long URL").'</label></p><hr />';}
	$content .= '<p><label><input type="checkbox" name="showthumbs" value="yes" '. (setting_fetch('showthumbs', 'yes') == 'yes' ? ' checked="checked" ' : '') .' /> '.__("Preview Photos In Timelines").'</label></p><hr>';
	$content .= '<p>'.__("Fixed Tags").': <input type="text" id="fixedtagspre" name="fixedtagspre" value="'.setting_fetch('fixedtagspre').'" maxlength="70" style="width:40px;" /> '.__("Tweet Content").' <input type="text" id="fixedtagspost" name="fixedtagspost" value="'.setting_fetch('fixedtagspost').'" maxlength="70" style="width:40px;" /><br /><small>'.__("Intro: Add Tags in Your Tweets").'</small></p><hr />';
	$content .= '<p>'.__("RT Syntax").':<br /><input type="text" id="rtsyntax" name="rtsyntax" value="'.setting_fetch('rtsyntax', 'RT [User]: [Content]').'" maxlength="140" /><br /><small>'.__("Default RT Syntax: RT [User]: [Content]").'</small></p><hr />';
	$content .= '<p>'.__("Customization CSS").':<br /><textarea name="css" cols="50" rows="3" id="css" style="width:95%">'.setting_fetch('css').'</textarea></p><hr />';
	$content .= '<p>'.__("Language").':<br /><select name="locale">'.theme('options', $locale, setting_fetch('locale', 'zh_CN')).'</select></p><hr />';
	$content .= '<p><label><input type="checkbox" name="reverse" value="yes" '. (setting_fetch('reverse') == 'yes' ? ' checked="checked" ' : '') .' /> '.__("Attempt to reverse the conversation thread view.").'</label></p>';
	$content .= '<div><input type="submit" name="Submit" value="'.__("Save").'" /></div></form><hr /><p>'.__("Visit <a href=\"reset\">Reset</a> if things go horribly wrong - it will log you out and clear all settings.").'</p>';
	return theme('page', __("Settings"), $content);
}

function settings_refresh($page = NULL) {
	if (isset($page)) {
		$page = BASE_URL . $page;
	} else {
		$page = $_SERVER['HTTP_REFERER'];
	}
	header('Location: '. $page);
	exit();
}
